<?php
/**
* @file Commentupm.php
* @synopsis  评论 顶一下
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015年7月7日18:54:18
*/

defined('BASEPATH') OR exit('No direct script access allowed');

class Commentupm extends CI_Model
{
	function __construct()
	{
	}

	//插入点赞
    public  function insert_commentup($sets)
    {
        $this->inc_comment_praise_num($sets['comment_id']);
        $this->db->insert("ml_commentup",$sets);
        return $this->db->insert_id();
    }

    //删除点赞(只删除一条)
    public function remove_commentup($query)
    {
        if($query)  // 不能为空
        {
            $this->reduce_comment_praise_num($query['comment_id']);
            $result = $this->db->where($query)->delete("ml_commentup");     //只删除一条
            return $result; 
        }
    }

    //删除点赞(删除多条)
    public function remove_commentup_batch($query)
    {
        if($query)  // 不能为空
        {
            $result = $this->db->where($query)->delete("ml_commentup");     //只删除一条
            return $result; 
        }
    }

    //获取点赞数
    public function get_commentup_count($query)
    {
        return $result = $this->db->where($query)->count_all_results("ml_commentup");
        //return $result = $this->cimongo->count_all("commentup");
    }

    //获取点赞列表
    public function get_comment_id($query,$limit,$offset)
    {
        //
        return $post_id_array = $this->db->where($query)->order_by("addtime", "desc")->get("ml_commentup",$limit, $offset)->result_array();

    }
//
//
//    //更新评论点赞日志
//    public function insert_commentup_log($uid,$comment_id)
//    {
//        $cg_data = array();
//        $cg_data['user_id'] = $uid;
//        $cg_data['comment_id'] = $comment_id;
//        $cg_data['addtime'] = time();
//        return $result = $this->cimongo->insert("commentup_log",$cg_data);
//    }

    // 判断用户是否日志赞过
    public function get_uid_commentup_log_count($query)
    {
        return $result = $this->db->where($query)->count_all_results("ml_commentup");
    }

    public function set_comment_is_answer($comment_id,$post_id)
    {
        $this->db->set('is_answer', '1', FALSE);
        $this->db->where(array("id"=>(int)$comment_id, "post_id"=>$post_id));
        $result = $this->db->update("ml_comment");
        return $result;
    }
    public function set_comment_is_noanswer($comment_id,$post_id)
    {
        $this->db->set('is_answer', '0', FALSE);
        $this->db->where(array("id"=>(int)$comment_id, "post_id"=>$post_id));
        $result = $this->db->update("ml_comment");
        return $result;
    }

    public function inc_comment_praise_num($comment_id)
    {
        $this->db->set('praise_count', 'praise_count+1', FALSE);
        $this->db->where(array("comment_id"=>(int)$comment_id));
        $result = $this->db->update("ml_praise_log");
        return $result;
    }
    public function reduce_comment_praise_num($comment_id)
    {
        $this->db->set('praise_count', 'praise_count-1', FALSE);
        $this->db->where(array("comment_id"=>(int)$comment_id));
        $result = $this->db->update("ml_praise_log");
        return $result;
    }
    public function update_comment_praise_num($comment_id,$praise_count)
    {
        $this->db->set('praise_count', $praise_count);
        $this->db->where(array("comment_id"=>(int)$comment_id));
        $result = $this->db->update("ml_praise_log");
        return $result;
    }
    public function insert_comment_praise_init($comment_id,$post_id,$author_id)
    {
        $data = array();
        $data['comment_id'] = $comment_id;
        $data['post_id'] = $post_id;
        $data['author_id'] = $author_id;
        $data['praise_count'] = 0;
        $this->db->insert("ml_praise_log",$data);
    }

}
